package com.huang.community.common.Handler;

import com.huang.community.common.annotation.MaxImage;
import com.huang.community.model.RespBean;
import com.huang.community.util.CommunityUtil;
import lombok.extern.slf4j.Slf4j;


import org.springframework.web.bind.annotation.ExceptionHandler;

import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 全局异常拦截器
 *  @author TianTian
 * @date 2022/4/25 21:02
 */
@RestControllerAdvice
@Slf4j

public class GlobalExceptionHandler {

    //拦截所有Exception
    @ExceptionHandler(value = Exception.class)
    public void handleBusinessException(Exception e, HttpServletRequest request, HttpServletResponse response) throws IOException {
        log.error("服务器发生异常" + e.getMessage());
        for (StackTraceElement stackTraceElement : e.getStackTrace()) {
            log.error(stackTraceElement.toString());
        }
        //判断是否为异步请求
        String header = request.getHeader("x-requested-with");
        if ("XMLHttpRequest".equals(header)) {
            response.setContentType("application/plain;charset=UTF8");
            PrintWriter writer = response.getWriter();
            writer.write(CommunityUtil.getJSONString(500, "请求错误"));

        } else {
            response.sendRedirect(request.getContextPath() + "/error");
        }
    }
//    @ExceptionHandler(value = MultipartException.class)
//    @ResponseBody
//    public RespBean handleBusinessException(MaxUploadSizeExceededException ex) {
//        String msg;
//        if (ex.getCause().getCause() instanceof FileSizeLimitExceededException) {
//            log.error(ex.getMessage());
//            msg = "上传文件过大[单文件大小不得超过10M]";
//        } else if (ex.getCause().getCause() instanceof SizeLimitExceededException) {
//            log.error(ex.getMessage());
//            msg = "上传文件过大[总上传文件大小不得超过10M]";
//        } else {
//            msg = "上传文件失败";
//        }
//        return RespBean.error(msg);
//    }
//}
//
//    @ExceptionHandler(value = MultipartException.class)
//    @ResponseBody
//    public ModelAndView handleBusinessException(MaxUploadSizeExceededException ex) {
//        String msg;
//        if (ex.getCause().getCause() instanceof FileSizeLimitExceededException) {
//            log.error(ex.getMessage());
//            msg = "上传文件过大[单文件大小不得超过10M]";
//        } else if (ex.getCause().getCause() instanceof SizeLimitExceededException) {
//            log.error(ex.getMessage());
//            msg = "上传文件过大[总上传文件大小不得超过10M]";
//        } else {
//            msg = "上传文件失败";
//        }
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.setViewName("/site/setting");
//        modelAndView.addObject("error","文件过大");
//        return modelAndView;
//
//    }

//    @ExceptionHandler(value = MultipartException.class)
//    @ResponseBody
//    public void handleBusinessException(MaxUploadSizeExceededException ex, HttpServletRequest request, HttpServletResponse response) throws IOException {
//        String msg;
//        if (ex.getCause().getCause() instanceof FileSizeLimitExceededException) {
//            log.error(ex.getMessage());
//            msg = "上传文件过大[单文件大小不得超过10M]";
//        } else if (ex.getCause().getCause() instanceof SizeLimitExceededException) {
//            log.error(ex.getMessage());
//            msg = "上传文件过大[总上传文件大小不得超过10M]";
//        } else {
//            msg = "上传文件失败";
//        }
//        String xRequestedWith = request.getHeader("x-requested-with");
//        if ("XMLHttpRequest".equals(xRequestedWith)) {
//            response.setContentType("application/plain;charset=utf-8");
//            PrintWriter writer = response.getWriter();
//            writer.write(CommunityUtil.getJSONString(1, msg));
//        } else {
//            response.sendRedirect(request.getContextPath() + "/error");
//        }
//
//    }
}
